package com.mc.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.mc.model.Teacher;
import com.mc.model.newsandbulletin;
import com.mc.service.NewsandbulletService;
import com.mc.service.TeacherService;
import com.mc.utils.StringUtil;

@Controller
public class CommonController {

	@Autowired
	private NewsandbulletService ns;
	@Autowired
	private TeacherService tservice;
	@RequestMapping("/index")
	public ModelAndView Home(){
		ModelAndView mav = new ModelAndView();
		mav.addObject("path", "index");
		mav.setViewName("home");
		return mav;
	}
	@RequestMapping("/frontPage/{path}")
	public ModelAndView showPage(@PathVariable("path")String path){
		ModelAndView mav = new ModelAndView();
		System.out.println("path:"+StringUtil.getPageName(path));
		String viewName = "";
		//mav.addObject("path", path);
		switch (path) {
		case "djzc":
			mav.addObject("news", ns.GetNewsByType(newsandbulletin.DJ_NEWS));
			mav.addObject("bulletins", ns.getBulletinByType(newsandbulletin.DJ_BULLETIN));
			break;
		case "teacherList":
			mav.addObject("computer", tservice.getTeacherByMajorName("计算机系"));
			mav.addObject("soft", tservice.getTeacherByMajorName("软件工程系"));
			mav.addObject("info", tservice.getTeacherByMajorName("计算机信息技术系"));
			mav.addObject("math", tservice.getTeacherByMajorName("应用数学系"));
			mav.addObject("wulian", tservice.getTeacherByMajorName("物联网"));
			mav.addObject("yjs", tservice.getTeacherByTeacherType("硕士生导师"));
			mav.addObject("out", tservice.getTeacherByTeacherType("优秀人才"));
			break;
		case "studentWork":
			mav.addObject("news", ns.GetNewsByType(newsandbulletin.STUDENT_NEWS));
			mav.addObject("bulletins", ns.getBulletinByType(newsandbulletin.STUDENT_BULLETIN));
			break;
		case "news":
			viewName = "redirect:/news/1";
			break;
		case "zsjy":
			mav.addObject("news", ns.GetNewsByType(newsandbulletin.ZSJY_NEWS));
			mav.addObject("bulletins", ns.getBulletinByType(newsandbulletin.ZSJY_BULLETIN));
			break;
		case "bulletin":
			viewName = "redirect:/bulletin/1";
			break;
		default:
			break;
		}
		if(viewName.equals("")){
			viewName = "frontView/"+StringUtil.getPageName(path);
		}
		System.out.println("view:"+viewName);
		mav.setViewName(viewName);
		return mav;
	}
	
	@RequestMapping("/news/{page}")
	public ModelAndView newsScan(@PathVariable Integer page){
		ModelAndView mav = new ModelAndView();
		mav.addObject("page", page);
		mav.addObject("pageSize", 8);
		int pageCount = 0;
		int count = ns.CountNewsByType(newsandbulletin.COMMON_NEWS);
		if(count%8 == 0){
			pageCount = count/8;
		}else{
			pageCount = count/8 + 1;
		}
		mav.addObject("pageCount", pageCount);
		mav.addObject("news", ns.getCommonNews(page, 8));
		mav.setViewName("frontView/"+StringUtil.getPageName("news"));
		return mav;
	}
	
	@RequestMapping("showDetail/{path}")
	public String showDetail(@PathVariable String path){
		return "static/"+path;
	}
	
	@RequestMapping("/bulletin/{page}")
	public ModelAndView bulletinScan(@PathVariable Integer page){
		ModelAndView mav = new ModelAndView();
		mav.addObject("cur", page);
		mav.addObject("pageSize", 8);
		int pageCount = 0;
		int count = ns.CountBulletinByType(newsandbulletin.COMMON_NEWS);
		if(count%8 == 0){
			pageCount = count/8;
		}else{
			pageCount = count/8 + 1;
		}
		mav.addObject("pageCount", pageCount);
		mav.addObject("bulletin", ns.getCommonBulletin(page, 8));
		mav.setViewName("frontView/"+StringUtil.getPageName("bulletin"));
		return mav;
	}
	
	@RequestMapping("Detail/{id}")
	public ModelAndView GetDetail(@PathVariable Integer id){
		ModelAndView mav = new ModelAndView();
		newsandbulletin news = ns.selectByPrimaryKey(id);
		mav.addObject("s", news);
		mav.addObject("title",news.getTitle()+"-西华大学-数学与计算机学院");
		mav.setViewName("showDetail");
		return mav;
	}
	
	@RequestMapping("teacherDetail/{id}")
	public ModelAndView GetTeacherDetail(@PathVariable Integer id){
		ModelAndView mav = new ModelAndView();
		Teacher t = tservice.selectByPrimaryKey(id);
		System.out.println(t);
		mav.addObject("t", t);
		mav.addObject("title",t.getName()+"-西华大学-数学与计算机学院");
		mav.setViewName("showDetail");
		return mav;
	}
}
